<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ServiceBank;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ServiceBankRepository;
use App\Http\Requests\Admin\ServiceBank\ShowRequest;
use App\Http\Requests\Admin\ServiceBank\QueryResource;
use App\Http\Requests\Admin\ServiceBank\ServiceBankCreate;
use App\Http\Requests\Admin\ServiceBank\ServiceBankUpdate;
use App\Http\Requests\Admin\ServiceBank\ServiceBankDestroy;

class ServiceBankController extends Controller
{
    protected $serviceBankRepository;

    /**
     * ServiceBanksController constructor.
     * @param ServiceBankRepository $serviceBankRepository
     */
    public function __construct(
        ServiceBankRepository $serviceBankRepository
    )
    {
        $this->serviceBankRepository = $serviceBankRepository;
    }

    /**
     * 获取快递员列表资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $service_id = $request->get('area_id', false);
        $limit = $request->get('paginate', false);
        $name = $request->get('name', false);
        $phone = $request->get('phone', false);
        $serviceBanks = $this->serviceBankRepository->getServiceBanks($service_id, $limit, $name, $phone);
        return success($serviceBanks);
    }

    /**
     * 获取快递员详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, ServiceBank $serviceBank)
    {
        return success($serviceBank);
    }


    /**
     * 创建快递员
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ServiceBankCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(ServiceBankCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $serviceBanks = $this->serviceBankRepository->create($attributes);
        return stored($serviceBanks);
    }

    /**
     * 更新快递员资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param ServiceBankUpdate $request
     * @param serviceBank $serviceBank
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ServiceBankUpdate $request, ServiceBank $serviceBank)
    {
        $serviceBanksAttr = $request->only([
            'service_id',
            'bank',
            'bank_name',
            'real_name',
            'bank_card',
            'phone',
            'status',
        ]);
        $serviceBank->update($serviceBanksAttr);
        return updated($serviceBank);
    }

    /**
     * 删除 快递员 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ServiceBankDestroy $request
     * @param ServiceBank $serviceBank
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(ServiceBankDestroy $request, ServiceBank $serviceBank)
    {
        $force = $request->get('force');
        $force ? $serviceBank->delete() : $serviceBank->forceDelete();
        return deleted();
    }
}
